<template>
	<view class="order_page">
		<uni-top fixed="true" statusBar="true" class="top" border="" background-color="#585cf7" color="#fff"
			left-text="订单" rightText="录入新单" @clickRight="addNewOrder">
		</uni-top>
		<!--end head-->
		<view class="nav_wrap" v-if="!hasLogin">
			<ren-dropdown-filter ref="child" :filterData='filterData' :defaultIndex='defaultIndex'
				@onSelected='onSelected'></ren-dropdown-filter>
		</view>
		<view class="nav_wrap" v-else>
			<block v-if="filterData[3]">
				<ren-dropdown-filter v-if="filterData[0].length>1" ref="child" :filterData='filterData'
					:defaultIndex='defaultIndexSub' @onSelected='onSelected'></ren-dropdown-filter>
			</block>
			<block v-else>
				<ren-dropdown-filter v-if="filterData[0].length>1" ref="child" :filterData='filterData'
					:defaultIndex='defaultIndex' @onSelected='onSelected'></ren-dropdown-filter>
			</block>
		</view>
		<!--暂无数据-->
		<view class="nocontent" v-if="orderList.length == 0">
			<image src="/static/image/public/WSJ.png" class="wsjImg"></image>
			<view>暂无数据</view>
		</view>

		<view class="page_list" v-else>
			<view class="list_item" v-for="item in orderList" :key="item.id" @click="goToReview(item.orderNumber)">
				<view class="top">
					<view class="left">
						<text class="orderNum">{{item.orderNumber}}</text>
						<text class="productName">{{item.productName}}</text>
					</view>
					<view class="right">
						<text class="time">{{item.day}}</text>
					</view>
				</view>
				<view class="center">
					<view class="left">
						<text class="name">{{item.borrowerName}}</text>
						<text class="status" :class="`status${item.status}`">{{item.statusName}}</text>
					</view>
					<view class="right">
						<block v-if="item.money">
							￥<text class="money">{{item.money}}</text> 万
						</block>
					</view>
				</view>
				<view class="address">
					{{item.address}}
				</view>
				<view class="bottom">
					<image class="img" src="../../static/image/page/time.png" mode="aspectFit"></image>
					<text class="text">本订单待放款完毕后核算业绩提成及奖金</text>
				</view>
			</view>
		</view>
		<!--end  pageList-->
		<uni-load-more :status="status" v-if="pageSize > 1" />
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters
	} from 'vuex';
	const app=getApp();
	export default {
		computed: {
			...mapState(["userInfo"]),
			...mapGetters(["hasLogin"]),
		},
		data() {
			return {
				filterData: [
					[{
						text: '全部产品',
						value: ''
					}],
					[{
							text: '全部',
							value: "全部",
							index: ""
						}, {
							text: '近三个月',
							value: '近三个月',
							index: 3
						}, {
							text: '近半年',
							value: "近半年",
							index: 6
						},
						{
							text: '近一年',
							value: "近一年",
							index: 12
						}
					],
					[{
						text: '全部状态',
						value: ''
					}]
				],
				defaultIndexSub: [0, 0, 0, 0],
				defaultIndex: [0, 0, 0],
				orderList: [],
				queryData: {},
				pageSize: 0,
				pageNum: 0,
				status: 'more',
			}
		},
		onLoad() {

		},
		onShow() {
			this.pageSize = 0;
			this.pageNum = 0;
			this.status= 'more';
			if (!this.hasLogin) return;
			this.getBaseInfor();
			console.log(app.globalData.showDialog)
		},
		onReachBottom: function() {
			if (!this.hasLogin) return;
			let that = this;
			let pageNum = this.pageNum + 1;
			if (pageNum <= this.pageSize) {
				that.queryData.pageNo = pageNum;
				that.getMoreData(that.queryData)
			} else {
				that.status = "noMore"
			}
		},
		methods: {
			//录入新单
			addNewOrder() {
				uni.navigateTo({
					url:'/pages/orderInside/addOrder'
				})
			},
			//上拉刷新
			onPullDownRefresh() {
				if (!this.hasLogin) {
					setTimeout(function() {
						uni.stopPullDownRefresh();
					}, 1000);
					return;
				}
				let that = this;
				let query = JSON.parse(JSON.stringify(this.queryData));
				query.pageNo = 1;
				that.$api.getOrderList(query).then(res => {
					that.orderList = res.data.data.records;
					that.pageSize = res.data.data.pages;
					that.pageNum = res.data.data.current;
					setTimeout(function() {
						uni.stopPullDownRefresh();
					}, 1000);
				}).catch(() => {
					setTimeout(function() {
						uni.stopPullDownRefresh();
					}, 1000);
				})
			},
			//选项卡筛选
			onSelected(res) {
				let reQuery = {};
				reQuery.pid = res[0][0].value;
				reQuery.month = res[1][0].index;
				reQuery.status = res[2][0].value;
				//是合伙人获取经纪人数据
				if (this.userInfo.teamPartnerStatus == 1) {
					reQuery.eid = res[3][0].value;
				}
				// 去除对象内多余的空值key
				for (let key in reQuery) {
					if (reQuery[key] === '') {
						delete reQuery[key]
					}
				}
				this.queryData = JSON.parse(JSON.stringify(reQuery));
				if (!this.hasLogin) return;
				this.getOrderList(reQuery);
				this.ToTop();
			},
			//获取 产品类型,经纪人数据
			async getBaseInfor() {
				let that = this;
				let reQuery = {};
				//获取产品
				let resDataPro = await this.$api.getProductList();
				let arrList = resDataPro.data.data;
				this.filterData[0] = [{
					value: '',
					text: '全部产品'
				}, ...arrList];

				//获取订单状态
				let resDataOrder = await this.$api.getOrderState();
				let arrListOrd = resDataOrder.data.data;
				this.filterData[2] = [{
					value: '',
					text: '全部状态'
				}, ...arrListOrd];

				//判断是否是合伙人
				//是合伙人获取经纪人数据
				if (this.userInfo.teamPartnerStatus == 1) {

					//获取全部经纪人数据
					let resDataTeam = await that.$api.getTeamList()
					let arr = JSON.parse(JSON.stringify(resDataTeam.data.data));
					arr.map((item, index) => {
						item.text = item.empName || item.phone;
						item.value = item.id;
					})
					this.filterData[3] = [{
						value: '',
						text: '全部经纪人'
					}, ...arr];
				}
				this.$forceUpdate();
				this.getOrderList(reQuery);
			},
			//根据条件获取订单列表
			getOrderList(reQuery) {
				this.$api.getOrderList(reQuery).then(res => {
					this.orderList = res.data.data.records;
					this.pageSize = res.data.data.pages;
					this.pageNum = res.data.data.current;
				});
			},
			//获取更多订单
			getMoreData(reQuery) {
				this.$api.getOrderList(reQuery).then(res => {
					let data = res.data.data.records;
					if (data) {
						this.orderList = this.orderList.concat(data);
						this.pageSize = res.data.data.pages;
						this.pageNum = res.data.data.current;
					}
				})
			},
			//回到顶部
			ToTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 500
				})
			},
			//调到订单详情页面
			goToReview(orderNum) {
				uni.navigateTo({
					url: "/pages/orderInside/orderReview?orderNum=" + orderNum
				})
			}
		},
		 /**
		   * 生命周期函数--监听页面隐藏
		   */
		  onHide: function () {
		    app.globalData.showDialog=false;
		  },
		
		  /**
		   * 生命周期函数--监听页面卸载
		   */
		  onUnload: function () {
		    app.globalData.showDialog=false;
		  },
	}
</script>

<style scoped lang="scss">
	.right_wrap {
		width: 200rpx;
	}

	.nav_wrap {
		width: 100%;
		height: 90rpx;
		background: $dtg-color-primary;
	}

	.page_list {
		padding: 0 30rpx;
		margin-top: 20rpx;
	}

	.list_item {
		padding: 20rpx;
		background: #FFFFFF;
		box-shadow: 0px 0px 38rpx 2rpx rgba(0, 0, 0, 0.04);
		border-radius: 20rpx;
		margin-bottom: 20rpx;

		.top {
			height: 58rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-bottom: 16rpx;
			border-bottom: 2rpx solid #EEEEEE;

			.orderNum {
				font-size: 26rpx;
				font-weight: 500;
				color: #333333;
			}

			.productName {
				border: 1px solid $dtg-color-primary;
				border-radius: 4rpx;
				font-size: 22rpx;
				font-weight: 500;
				color: $dtg-color-primary;
				margin-left: 20rpx;
				padding: 0 8rpx;
			}

			.time {
				font-size: 22rpx;
				color: #999999
			}
		}

		.center {
			height: 80rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.name {
				font-size: 32rpx;
				font-weight: bold;
				color: #333333;
			}

			.status {
				background: $dtg-color-primary;
				border-radius: 4rpx;
				font-size: 22rpx;
				color: #FFFFFF;
				padding: 0 10rpx;
				margin-left: 20rpx;
				line-height: 32rpx;
			}

			.status1 {
				background: #FF9500
			}

			.status2 {
				background: #FF9500
			}

			.status3 {
				background: #28B779
			}

			.status4 {
				background: #F96363
			}

			.status5 {
				background: #28B779
			}

			.status6 {
				background: #F96363
			}

			.money {
				font-weight: bolder;
				font-size: 42rpx;
			}
		}

		.address {
			font-size: 26rpx;
			font-weight: 500;
			color: #333333;
			line-height: 40rpx;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}

		.bottom {
			height: 60rpx;
			background: #F8F8F8;
			margin-top: 12rpx;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			padding: 0 20rpx;

			.img {
				width: 26rpx;
				height: 26rpx;
				margin-right: 20rpx;
			}

			.text {
				font-size: 22rpx;
				font-weight: 500;
				color: #999999;
			}
		}
	}

	.nocontent {
		width: 100%;
		font-size: 15px;
		text-align: center;
		padding-top: 160rpx;
		color: #999999;
		line-height: 50px;

		.wsjImg {
			width: 180px;
			height: 125px;
			margin: 0 auto;
		}
	}
</style>
